﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;

namespace Beway.ValueAdded.Util
{
    public class Encryption
    {

        /// <summary>
        /// Des加密
        /// </summary>
        /// <param name="pToEncrypt"></param>
        /// <param name="strkey"></param>
        /// <returns></returns>
        public static string DESEncrypt(string pToEncrypt, string strkey)
        {

            using (DESCryptoServiceProvider des = new DESCryptoServiceProvider())
            {
                byte[] inputByteArray = Encoding.UTF8.GetBytes(pToEncrypt);
                //des.Key = Encoding.ASCII.GetBytes(strkey.Substring(0, 8));	
                //des.Key = Convert.FromBase64String(strkey);
                des.Key = Convert.FromBase64String(strkey.Substring(0, 12));
                des.IV = Convert.FromBase64String(strkey.Substring(12));
                des.Mode = CipherMode.CBC;
                des.Padding = PaddingMode.PKCS7;
                System.IO.MemoryStream ms = new System.IO.MemoryStream();
                using (CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write))
                {
                    cs.Write(inputByteArray, 0, inputByteArray.Length);
                    cs.FlushFinalBlock();
                    cs.Close();
                }
                string str = Convert.ToBase64String(ms.ToArray());
                ms.Close();
                return str;
            }

        }

        /// <summary>
        /// 解密
        /// </summary>
        /// <param name="pToDecrypt"></param>
        /// <param name="strkey"></param>
        /// <returns></returns>
        public static string DESDecrypt(string pToDecrypt, string strkey)
        {
            try
            {
                byte[] inputByteArray = Convert.FromBase64String(pToDecrypt);
                using (DESCryptoServiceProvider des = new DESCryptoServiceProvider())
                {
                    des.Key = Convert.FromBase64String(strkey.Substring(0, 12));
                    des.IV = Convert.FromBase64String(strkey.Substring(12));
                    des.Mode = CipherMode.CBC;
                    des.Padding = PaddingMode.PKCS7;
                    System.IO.MemoryStream ms = new System.IO.MemoryStream();
                    using (CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write))
                    {
                        cs.Write(inputByteArray, 0, inputByteArray.Length);
                        cs.FlushFinalBlock();
                        cs.Close();
                    }
                    string str = Encoding.UTF8.GetString(ms.ToArray());
                    ms.Close();
                    return str;
                }
            }
            catch
            {
                return "";
            }
        }
        /// <summary>
        /// SHA256加密
        /// </summary>
        /// <param name="pToEncrypt"></param>
        /// <returns></returns>
        public static string SHA256Encrypt(string pToEncrypt)
        {

            using (SHA256CryptoServiceProvider sha256 = new SHA256CryptoServiceProvider())
            {
                byte[] byteArr = Encoding.UTF8.GetBytes(pToEncrypt);
                byte[] cropTo = sha256.ComputeHash(byteArr);
                string result = GetStringValue(cropTo).ToLower();
                return result;
            }
        }
        private static string GetStringValue(byte[] bytes)
        {
            string text = "";
            if (bytes != null)
            {
                for (int i = 0; i < bytes.Length; i++)
                {
                    text += bytes[i].ToString("X2");
                }
            }
            return text;
        }
        /// <summary>
        /// base64加密
        /// </summary>
        /// <param name="ctb"></param>
        /// <returns></returns>
        public static string Base64Encode(string strToBase64)
        {
            byte[] bt = Encoding.UTF8.GetBytes(strToBase64);
            string base64 = Convert.ToBase64String(bt);
            return base64;
        }
        /// <summary>
        /// base64解码
        /// </summary>
        /// <param name="Decode"></param>
        /// <returns></returns>
        public static string Base64Decode(string Decode)
        {
            byte[] bt = Convert.FromBase64String(Decode);
            string dCode = Encoding.UTF8.GetString(bt);
            return dCode;

        }
    }
}
